package com.xzx.controller;

import com.xzx.dto.CommonPage;
import com.xzx.dto.SuccessResponseDTO;
import com.xzx.dto.Tuple2;
import com.xzx.po.RolePO;
import com.xzx.po.UserPO;
import com.xzx.service.UserService;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.*;

import java.util.HashMap;
import java.util.List;
import java.util.Map;

/**
 *
 * @author xinzhixuan
 * @version 1.0
 * @date 2025/8/18 14:08
 */
@Slf4j
@RequestMapping("/user")
@Controller
public class UserController {

    @Autowired
    private UserService userService;

    @GetMapping("/list")
    public String list(@RequestParam(name = "query", required = false) String query,
                       @RequestParam(name = "page", required = false, defaultValue = "1") int  page,
                       @RequestParam(name = "size", required = false, defaultValue = "2") int  size,
                       Model model) {
        CommonPage<UserPO> list = userService.list(query, page, size);
        model.addAttribute("result", list);
        model.addAttribute("query", query);
        return "user/list";
    }

    @ResponseBody
    @PostMapping("/doAdd")
    public String doAdd(@RequestBody UserPO userPO) {
        userService.doAdd(userPO);
        return "ok";
    }

    @ResponseBody
    @PostMapping("/doDelete")
    public SuccessResponseDTO doDelete(@RequestParam("ids") List<Integer> ids) {
        userService.delete(ids);
        return SuccessResponseDTO.of();
    }

    @GetMapping("/edit")
    public String edit(@RequestParam("id") Integer id, Model model) {
        UserPO userPO = userService.getById(id);
        model.addAttribute("user", userPO);
        return "user/edit";
    }

    @ResponseBody
    @PostMapping("/doEdit")
    public SuccessResponseDTO doEdit(@RequestBody UserPO userPO) {
        log.info("doEdit userPO:{}", userPO);
        userService.doEdit(userPO);
        return SuccessResponseDTO.of();
    }

    @GetMapping("/assignRole")
    public String assignRole(@RequestParam("id") Long id, Model model) {
        Tuple2<List<RolePO>, List<RolePO>> tuple2 = userService.selectAllRole(id);
        model.addAttribute("assignRoles", tuple2.getF1());
        model.addAttribute("unassignRoles", tuple2.getF2());
        return "user/assignRole";
    }

    @ResponseBody
    @PostMapping("/doAssign")
    public SuccessResponseDTO doAssign(@RequestParam("userId") Integer userId,
                                       @RequestParam("roleIds") List<Long> roleIds){
        userService.doAssign(userId, roleIds);
        return SuccessResponseDTO.of();
    }

    @ResponseBody
    @PostMapping("/doUnassign")
    public SuccessResponseDTO doUnassign(@RequestParam("userId") Integer userId,
                                       @RequestParam("roleIds") List<Long> roleIds){
        userService.doUnassign(userId, roleIds);
        return SuccessResponseDTO.of();
    }

}
